package com.panda.pandawash.application;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.panda.pandawash.R;
import com.panda.pandawash.bean.UserBean;
import com.panda.pandawash.util.CDPreferenceOperateUtils;

import org.xutils.x;

import cn.sharesdk.framework.ShareSDK;

public class CDApplication extends Application {
    private static CDApplication instance;
    private static CDPreferenceOperateUtils preferenceOperateUtils;
    private UserBean user;
    private final String updateMD5 = "B9B8B6B68D11E132515B5AEC243E4D6D";

    //移植
    private DbUtils dbUtils;
    private int LogInStatus = -1;
    private String UserID = "";
    private String UserMAC = "";


    public DbUtils getDbUtil() {
        if(this.dbUtils == null) {
            this.dbUtils = DbUtils.create(this);
        }

        return this.dbUtils;
    }

    public void setLogInStatus(int LogInStatus) {
        this.LogInStatus = LogInStatus;
    }

    public int getLogInStatus() {
        return this.LogInStatus;
    }

    public void setUserID(String UserID) {
        if(UserID != null && !UserID.equals("")) {
            this.UserID = UserID;
        } else {
            this.UserID = "未知设备";
        }

    }

    public String getUserID() {
        return this.UserID;
    }

    public void setUserMAC(String UserMAC) {
        this.UserMAC = UserMAC;
    }

    public String getUserMAC() {
        return this.UserMAC;
    }

    //
    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ShareSDK.initSDK(CDApplication.this);
           }
       }).start();
        instance = this;
        x.Ext.init(this);
        x.Ext.setDebug(true);
        initPreShare();
        initUser();

        // avoid android n camera error
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());


        this.dbUtils = DbUtils.create(this);
    }

    private void initPreShare() {
        if (null == preferenceOperateUtils)
            preferenceOperateUtils = new CDPreferenceOperateUtils(this);
    }


    public static CDApplication getIntance() {
        return instance;
    }


    public boolean isNewVersion() {
        String versionName = "1.0";
        try {
            versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (versionName.equals(getPreferenceUtils().getString("version", "0"))) {
                return false;
            } else {
                getPreferenceUtils().setString("version", versionName);
                return true;
            }
        }
    }
    public CDPreferenceOperateUtils getPreferenceUtils() {
        return preferenceOperateUtils;
    }

    /**
     * 获取用户登录状态
     */
    public boolean getLoginStatus() {
        return getPreferenceUtils().getBoolean("logged", false);
    }

    public void setLoginStatus(boolean isLogged) {
        getPreferenceUtils().setBoolean("logged", isLogged);
    }

    public UserBean getUserInfo() {
        return user;
    }

    private void initUser() {
        user = getPreferenceUtils().getConfig(UserBean.class);
        if (TextUtils.isEmpty(user.getUserpassword()) || user.getUserpassword().toString()=="") {
            getPreferenceUtils().setBoolean("logged", false);
        } else {
            getPreferenceUtils().setBoolean("logged", true);
        }
    }

    public void saveUserInfo(UserBean user) {
        if (!this.user.equals(user)) {
            this.user = user;
        }
        getPreferenceUtils().setConfig(user);
    }

    public void logout() {
        setLoginStatus(false);
        this.user = new UserBean();
        saveUserInfo(user);
    }

    public String getVersion() {
        try {
            PackageManager e = instance.getPackageManager();
            PackageInfo info = e.getPackageInfo(instance.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception var4) {
            var4.printStackTrace();
            return instance.getString(R.string.can_not_find_version_name);
        }
    }

    public int getVersionCode() {
        try {
            PackageManager e = instance.getPackageManager();
            PackageInfo info = e.getPackageInfo(instance.getPackageName(), 0);
            int versionCode = info.versionCode;
            return versionCode;
        } catch (Exception var4) {
            var4.printStackTrace();
            return 0;
        }
    }

    public String getUpdateMD5() {
        return "9b24c4c38c9ea9d6ce4fe8d384225fb2";
    }

    public boolean isUpdate(String now_version, String download_version) {
        if(now_version.equals(download_version)) {
            return false;
        } else {
            int now_v = Integer.valueOf(now_version.substring(1, 2)).intValue();
            int down_v = Integer.valueOf(download_version.substring(1, 2)).intValue();
            Log.i("bear_", now_v + "----" + down_v);
            if(now_v > down_v) {
                return false;
            } else if(now_v < down_v) {
                return true;
            } else {
                now_v = Integer.valueOf(now_version.substring(3, 4)).intValue();
                down_v = Integer.valueOf(download_version.substring(3, 4)).intValue();
                Log.i("bear_", now_v + "----" + down_v);
                if(now_v > down_v) {
                    return false;
                } else if(now_v < down_v) {
                    return true;
                } else {
                    now_v = Integer.valueOf(now_version.substring(4)).intValue();
                    down_v = Integer.valueOf(download_version.substring(4)).intValue();
                    Log.i("bear_", now_v + "----" + down_v);
                    return now_v < down_v;
                }
            }
        }
    }

    public boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService("connectivity");
        if(connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if(info != null && info.isConnected() && info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }

        return false;
    }


}
